<?php

/*
 * 题库管理
 */

session_start();
include_once dirname(__FILE__)."/../src/daos/chaptersdao.php";
include_once dirname(__FILE__)."/../src/daos/questionsdao.php";
include_once dirname(__FILE__)."/../src/objects/chapter.php";
include_once dirname(__FILE__)."/../src/objects/question.php";
include_once dirname(__FILE__)."/../conf/smarty.conf.php";

$chaptersdao = new ChapterSDAO();
$questiondao = new QuestionsDAO();
$question_chapter_id = $chaptersdao->getFirstChapter()->getChapterId();
if(isset($_REQUEST["question_chapter_id"])){
	$question_chapter_id = intval($_REQUEST["question_chapter_id"]);
}
if(isset($_POST["chapter_name"])){	
	// 添加分类
	$chapter_name = strval($_POST["chapter_name"]);
	if($chapter_name != ""){		
	$chapter_description = "";
		$chapter = new Chapter();
		$chapter->setChapterName($chapter_name);
		$chapter->setChapterDescription($chapter_description);
		$chaptersdao->insertChapter($chapter);
	}
}
if (isset($_POST["question_title"])&&
		isset($_POST["question_answer_a"])&&
		isset($_POST["question_answer_b"])&&
		isset($_POST["question_answer_c"])&&
		isset($_POST["question_answer_d"])&&
		isset($_POST["question_answer_correct"])&&
		isset($_POST["question_analytic"])) {
	// 添加试题
	$question_title = strval($_POST["question_title"]);
	$question_answer_a = strval($_POST["question_answer_a"]);
	$question_answer_b = strval($_POST["question_answer_b"]);
	$question_answer_c = strval($_POST["question_answer_c"]);
	$question_answer_d = strval($_POST["question_answer_d"]);
	$question_answer_correct = intval($_POST["question_answer_correct"]);
	$question_analytic = strval($_POST["question_analytic"]);
	$question = new Question();
	$question->setQuestionTitle($question_title);
	$question->setQuestionAnswerA($question_answer_a);
	$question->setQuestionAnswerB($question_answer_b);
	$question->setQuestionAnswerC($question_answer_c);
	$question->setQuestionAnswerD($question_answer_d);
	$question->setQuestionAnswerCorrect($question_answer_correct);
	$question->setQuestionAnalytic($question_analytic);
	$question->setQuestionChapterId($question_chapter_id);
	$questiondao->insertQuestion($question);
}
$chapters = $chaptersdao->getAllChapters();
$questions = $questiondao->getQuestionsByChapter($question_chapter_id);
$smarty->assign("chapters", $chapters );
$smarty->assign("questions", $questions );
$smarty->display("frm_questions_mng.html");
?>
